package com.xc.freeapp.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xc.freeapp.dao.BusinessfloorDao;
import com.xc.freeapp.dao.DepartmentDao;
import com.xc.freeapp.dao.HospitalDao;
import com.xc.freeapp.dao.dataSource.DBContextHolder;
import com.xc.freeapp.entity.Businessfloor;
import com.xc.freeapp.entity.Department;
import com.xc.freeapp.entity.Departments;
import com.xc.freeapp.entity.Hospital;
import com.xc.freeapp.service.HospitalService;
import com.xc.freeapp.util.PropertiesUtil;
import com.xc.freeapp.util.UploadUtil;

@Service
public class HospitalServiceImpl implements HospitalService {

	@Autowired 
	private HospitalDao hospitalDao;
	
	@Autowired
	private DepartmentDao departmentDao; 
	
	@Autowired
	private BusinessfloorDao businessfloorDao;
	
	public int deleteByPrimaryKey(Integer id) {
		DBContextHolder.switchDataSourceRW();//切换数据源
		return hospitalDao.deleteByPrimaryKey(id);
	}

	public int insert(Hospital record) {
		DBContextHolder.switchDataSourceRW();//切换数据源
		return hospitalDao.insert(record);
	}

	public Hospital selectByPrimaryKey(Integer id) {
		DBContextHolder.switchDataSourceRW();//切换数据源
		Hospital hospital =hospitalDao.selectByPrimaryKey(id);
		
		return hospital;
	}

	public int updateByPrimaryKey(Hospital record) {
		DBContextHolder.switchDataSourceRW();//切换数据源
		return hospitalDao.updateByPrimaryKey(record);
	}
	
	/**
	 * 根据医院id和科室类型获取科室列表(只查询主科室)
	 * @param paramsmap {"businessId","querytype"}
	 */
	public List<Department> getDeparmentList(Map<String, Object> paramsmap){
		DBContextHolder.switchDataSourceRW();//切换数据源
		return departmentDao.getDeparmentList(paramsmap);
	}
	/**
	 * 根据医院id获取科室列表
	 * @param paramsmap {"businessId"}
	 */
	public List<Department> getAllDeparment(Integer businessId){
		DBContextHolder.switchDataSourceRW();//切换数据源
		return departmentDao.getAllDeparment( businessId);
	}
	public Integer selectappstatus(Integer appid){
		DBContextHolder.switchDataSourceRW();//切换数据源
		return hospitalDao.selectappstatus(appid);
	}
	/**
	 * 查询所有科室,分层级显示
	 */
	public List<Departments> getDeparmentsTreeList(Map<String, Object> paramsmap){
		DBContextHolder.switchDataSourceRW();//切换数据源
		List<Departments> departments =new ArrayList<Departments>();
		List<Department> departmentList =departmentDao.getDeparmentTypeList(paramsmap);
		for (Department department : departmentList) {
			paramsmap.put("partentid", department.getId());
			List<Department> list =departmentDao.getDeparmentsList(paramsmap);
			Departments depart=new Departments();
			depart.setBusinessid(department.getBusinessid());
			depart.setCreateby(department.getCreateby());
			depart.setCreatetime(department.getCreatetime());
			depart.setDelflag(department.getDelflag());
			depart.setDlevel(department.getDlevel());
			depart.setId(department.getId());
			depart.setIntroduce(department.getIntroduce());
			depart.setName(department.getName());
			depart.setPartentid(department.getPartentid());
			depart.setPics(UploadUtil.urlConversion(department.getPics()));
			depart.setStatus(department.getStatus());
			depart.setUpdateby(department.getUpdateby());
			depart.setUpdatetime(department.getUpdatetime());
	
			depart.setDepartments(list);
			departments.add(depart);
		}
		return departments;
	}
	
	
	/**
	 * 根据医院id和大楼编码获取医院大楼楼层信息列表
	 * @param paramsmap {"businessId","code"} 
	 * */
	public List<Businessfloor> getFloorList(Map<String, Object> paramsmap) {
		DBContextHolder.switchDataSourceRW();//切换数据源
		return businessfloorDao.getList(paramsmap);
	}
	
}
